package com.appinterfacelite.service;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 在接口的基础上封装一层接口
 * 通过后台解决跨域问题
 */
@Service
public class InterfaceService {
    @Autowired
    RestTemplate restTemplate;

    @Value("${interface.ip}")
    public String ip;

    @Value("${interface.port}")
    public String port;

    public JSONObject channelList(String https, String parentId, String siteId, Boolean hasContentOnly, String first, String count) {
        String url = "http://" + ip + ":" + port + "/api/front/channel/list";
        Map<String, Object> params = new HashMap<>();
        params.put("https", https);
        params.put("parentId", parentId);
        params.put("siteId", siteId);
        params.put("hasContentOnly", hasContentOnly);
        params.put("first", first);
        params.put("count", count);
        return JSONObject.fromObject(restTemplate.getForObject(url +
                        "?https={https}&parentId={parentId}&siteId={siteId}&hasContentOnly={hasContentOnly}&first={first}&count={count}",
                String.class,
                params));
    }

    public JSONObject contentList(
            String format, String https, String ids, String tagIds, String channelIds,
            String siteIds, String typeIds, String titleImg, String recommend, String orderBy,
            String title, String channelOption, String first, String count) {
        String url = "http://" + ip + ":" + port + "/api/front/content/list";
        Map<String, Object> params = new HashMap<>();
        params.put("format", format);
        params.put("https", https);
        params.put("ids", ids);
        params.put("tagIds", tagIds);
        params.put("channelIds", channelIds);
        params.put("siteIds", siteIds);
        params.put("typeIds", typeIds);
        params.put("titleImg", titleImg);
        params.put("recommend", recommend);
        params.put("orderBy", orderBy);
        params.put("title", title);
        params.put("channelOption", channelOption);
        params.put("first", first);
        params.put("count", count);
        return JSONObject.fromObject(restTemplate.getForObject(url +
                        "?format={format}&https={https}&ids={ids}&tagIds={tagIds}&channelIds={channelIds}&" +
                        "siteIds={siteIds}&typeIds={typeIds}&titleImg={titleImg}&recommend={recommend}&" +
                        "orderBy={orderBy}&title={title}&channelOption={channelOption}&first={first}&count={count}",
                String.class, params));
    }

    public JSONObject contentGet(String format, String https, String sessionKey, String appId, String id, String next, String channelId, String siteId) {
        String url = "http://" + ip + ":" + port + "/api/front/content/get";
        Map<String, Object> params = new HashMap<>();
        params.put("format", format);
        params.put("https", https);
        params.put("sessionKey", sessionKey);
        params.put("appId", appId);
        params.put("id", id);
        params.put("next", next);
        params.put("channelId", channelId);
        params.put("siteId", siteId);
        return JSONObject.fromObject(restTemplate.getForObject(url +
                        "?format={format}&https={https}&sessionKey={sessionKey}&appId={appId}&id={id}&" +
                        "next={next}&channelId={channelId}&siteId={siteId}",
                String.class, params));
    }

//    public JSONObject test(){
//        Map<String,String> map=new HashMap<>();
//        map.put("ip",ip);
//        map.put("port",port);
//        return JSONObject.fromObject(map);
//    }
}